<template>
  <div :class="{'hidden':hidden}" class="pagination-container" :style="{justifyContent: isCenter? 'flex-end' : ''}">
    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :pager-count="pagerCount"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!--   size-change pageSize 改变时会触发-->
    <!--   currentPage 改变时会触发 当前页-->
  </div>
</template>

<script>
// import { scrollTo } from '@/utils/scroll-to'

export default {
  name: 'Pagination',
  // total总条目数
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 200
    },
    pageSizes: {
      type: Array,
      default() {
        return [20, 50, 200, 300, 500, 1000, 2000]
      }
    },
    // 页码按钮的数量，当总页数超过该值时会折叠
    pagerCount: {
      type: Number,
      default: 5
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    },
    isCenter: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('pagination', { page: this.currentPage, limit: val })
      // if (this.autoScroll) {
      //   scrollTo(0, 800)
      // }
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, limit: this.pageSize })
      // if (this.autoScroll) {
      //   this.$nextTick(() => {
      //     scrollTo(0, 800)
      //   })
      // }
    }
  }
}
</script>

<style scoped lang="scss">
.pagination-container {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  position: relative;
  background: #fff;
  padding: 5px 16px;
}
.hangFixed {
  font-size: 14px;
  color: #666;
  position: absolute;
  left: -120px;
}
.el-pagination{
  text-align: center;
  &::v-deep .el-pagination__jump {
    margin-left: 10px;
 }
  &::v-deep .el-pagination__total {
    margin-right: 10px;
  }
  &::v-deep span:not([class*=suffix]) {
    font-size: 12px;
    min-width: 35px;
    height: 28px !important;
    line-height: 28px;
  }
  &::v-deep .el-select .el-input {
    width: 100px;
    margin: 0 5px;
  }
  &::v-deep .el-input__inner, .el-input__icon {
    height: 2.576vh;
    line-height: 2.576vh;
  }
  &::v-deep .el-input__icon {
    height: 2.576vh;
    line-height: 2.576vh;
  }
  &::v-deep .el-pager li {
    padding: 0 4px;
  }
  &::v-deep .el-pager .more::before {
    line-height: 2.576vh;
  }
}
.pagination-container.hidden {
  display: none;
}

.el-pagination::v-deep .btn-prev,
.el-pagination::v-deep .btn-next,
.el-pagination::v-deep .el-pager li {
  font-size: 12px;
  min-width: 1.56vw;
  height: 28px !important;
  line-height: 28px;
  margin: 0 5px;
}
</style>
